﻿<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<CarHelpDesk.Web.Models.Answer>" %>

<% Html.EnableClientValidation(); %>

<div id="ajax_result"></div>
<div id="ask_question">
    <% using (Ajax.BeginForm("Create", "Answers", new AjaxOptions()
       {
           HttpMethod = "POST",
           OnSuccess = "onAnswerSuccess",
           OnFailure = "onAnswerFailure",
           OnBegin = "onAnswerSubmitBegin",
           OnComplete = "onAnswerSubmitComplete"

       }, new { id = "answer_form" }))
       {%>
    
    <fieldset>
        <legend>Добави своя отговор</legend>
        
        <%=Html.Hidden("questionID")%>
        
        <%= Html.ValidationMessageFor(model => model.Content)%>
        <div class="editor-field">
            <%= Html.TextAreaFor(model => model.Content, 6, 60, null)%>
            <%= Html.ValidationMessageFor(model => model.Content)%>
        </div>
        <br />
        <input id="submit_answer" type="submit" value="отговори" onclick="checkAuthentication(); return false;" />
    </fieldset>
    <% } %>
</div>

<script type="text/javascript">

    function userAuthenticated() {        
        // calling .submit() or .trigger('submit') does not invoke
        // the ajax form handler and results in a broken page
        $('<input type="submit" />').hide().appendTo('#answer_form').click();
    }

    function onAnswerSuccess(content) {
        $(content.get_data()).hide().appendTo('#ajax_result').slideDown("slow");
        $("#answer_form").find("#Content").val('');
    }

    function onAnswerFailure(content) {
        alert("Грешка при записването на отговора");
    }

    function onAnswerSubmitBegin(content) {
        $("#submit_answer").attr("disabled", true);
    }

    function onAnswerSubmitComplete(content) {
        $("#submit_answer").attr("disabled", false);
    }
    
</script>
